﻿using System;
using System.Collections;
using UnityEngine;

namespace Fish.Utility
{
	public class CoroutineUtility
	{
		public interface ICoroutine
		{
			void Start(IEnumerator enumerator);
			void Stop();
		}

		private class Coroutine : ICoroutine
		{
			private MonoBehaviour target;
			private IEnumerator enumerator;

			public Coroutine(MonoBehaviour target)
			{
				this.target = target;
			}

			public void Start(IEnumerator enumerator)
			{
				Stop();

				this.enumerator = enumerator;

				target.StartCoroutine(this.enumerator);
			}

			public void Stop()
			{
				if (enumerator != null)
					target.StopCoroutine(enumerator);
			}
		}

		public static ICoroutine Create(MonoBehaviour target)
		{
			return new Coroutine(target);
		}
	}
}
